/**
 * Created by thinkpad on 2017-06-01.
 */
window.XBox = window.XBox || { global: window };

XBox.notify = (function () {
    var create = function () {
        if (!XBox.notify.instence) {
            var notify = $("<span >");
            notify.kendoNotification({
                autoHideAfter: 3000,
                open: {
                    effects: "fade:in"
                },
                close: {
                    effects: "fade:out",
                    reverse: true
                },
                button: true,
                height: "4em",
                width: "20em",
                hideOnClick: false,
                position: {
                    pinned: true,
                    bottom: 50,
                    right: 50
                },
                templates: [
                    {
                        type: "success",
                        template:
                            '<div class="k-notification-wrap"><span class="k-icon k-i-success" title="success"></span><span class="notify-content">#= message #</span><span class="k-icon k-i-close" title="Hide"></span></div>'
                    },
                    {
                        type: "info",
                        template:
                            '<div class="k-notification-wrap"><span class="k-icon k-i-info" title="info"></span><span class="notify-content">#= message #</span><span class="k-icon k-i-close" title="Hide"></span></div>'
                    },
                    {
                        type: "warning",
                        template:
                            '<div class="k-notification-wrap"><span class="k-icon k-i-warning" title="warning"></span><span class="notify-content">#= message #</span><span class="k-icon k-i-close" title="Hide"></span></div>'
                    },
                    {
                        type: "error",
                        template:
                            '<div class="k-notification-wrap"><span class="k-icon k-i-error" title="error"></span><span class="notify-content">#= message #</span><span class="k-icon k-i-close" title="Hide"></span></div>'
                    }
                ]
            });
            XBox.notify.instence = notify.data("kendoNotification");
        }
        return XBox.notify.instence;
    }
    return {
        success: function (message) {
            var ntf = create();
            ntf.success({ message: message });
            return ntf;
        },
        info: function (message) {
            var ntf = create();
            ntf.info({ message: message });
            return ntf;
        },
        warning: function (message) {
            var ntf = create();
            ntf.warning({ message: message });
            return ntf;
        },
        error: function (message) {
            var ntf = create();
            ntf.error({ message: message });
            return ntf;
        }
    }
})();

XBox.window = function (options) {
    var defaultConfig = {
        actions: ["Minimize", "Maximize", "Close"],
        destroyWhenClose: true,//false只是关闭，true关闭时销毁
        autoOpen: true,
        animation: {
            open: {
                effects: "fade:in",
                duration: 200
            },
            close: {
                effects: "fade:out",
                duration: 200
            }
        },
        draggable: true,
        title: "窗口",
        width: 600,
        height: 400,
        minWidth: 100,
        //maxWidth: 900,
        minHeight: 100,
        //maxHeight: 500,
        modal: true,
        pinned: true,
        resizable: true,
        buttons: []
    };

    var defaultButton = {
        text: "按钮",
        name: "btn",
        icon: "",
        theme: "k-primary",//"k-primary",
        enabled: true,
        handler: null//名称或函数
    }

    this.options = XBox.apply({}, defaultConfig, options);

    this.options.open = null;

    this.title = function (title) {
        this.titleBar.find(".window-title").html(title);
    }

    var create = function (cfg) {
        var ctn = $("<div>");
        var wnd = ctn.kendoWindow(cfg).data("kendoWindow");
        this.titleBar = wnd.wrapper.find(".k-window-title");
        this.titleBar.empty();
        if (cfg.icon) {
            this.titleBar.append("<span class='" + cfg.icon + "'></span>");
        }
        this.titleBar.append("<span class='window-title'></span>");
        this.title(cfg.title);
        wnd.center();
        this.container = wnd.wrapper.find(".k-window-content");
        if (cfg.buttons && cfg.buttons.length) {
            this.container.css("height", "calc(100% - 53px)");
            this.footer =
                $('<div class="k-header k-window-footer"><div class="k-window-button"></div></div>');
            this.container.after(this.footer);

            for (var i = 0; i < cfg.buttons.length; i++) {
                var bcfg = XBox.apply({}, defaultButton, cfg.buttons[i]);
                var btn = $("<a></a>");
                this.footer.find(".k-window-button").append(btn);
                bcfg.theme && btn.addClass(bcfg.theme);
                bcfg.text && btn.html(bcfg.text);
                bcfg.name && btn.attr("name", bcfg.name);
                var opt = { enable: !!bcfg.enabled };
                bcfg.icon && (opt.spriteCssClass = bcfg.icon);
                btn.kendoButton(opt);
                (function (xwindow, bcfg, btn) {
                    var clk;
                    if (XBox.isFunction(bcfg.handler)) {
                        clk = function (e) { bcfg.handler.call(xwindow, btn, e) };
                    } else if (XBox.isString(bcfg.handler) && XBox.isFunction(cfg[bcfg.handler])) {
                        clk = function (e) { cfg[bcfg.handler].call(xwindow, btn, e) };
                    }
                    if (clk) {
                        btn.data("kendoButton").setOptions({ click: clk });
                    }
                })(this, bcfg, btn);
            }
        } else {
            this.container.css("height", "calc(100% - 14px)");
        }

        if (cfg.init && XBox.isFunction(cfg.init)) {
            cfg.init.apply(this);
        }

        return wnd;
    }
    this.kendo = create.apply(this, [this.options]);

    this.show = function () {
        this.kendo.open();
    }

    this.open = this.show;

    this.close = function () {
        this.kendo.close();
    }

    this.destroy = function () {
        this.kendo.destroy();
    }

    var toggleFooter = function () {
        this.footer.css("display", this.container.css("display"));
    }

    this.kendo.maximize = XBox.Function.postFunction(this.kendo.maximize, toggleFooter.bind(this), this.kendo);
    this.kendo.minimize = XBox.Function.postFunction(this.kendo.minimize, toggleFooter.bind(this), this.kendo);
    this.kendo.restore = XBox.Function.postFunction(this.kendo.restore, toggleFooter.bind(this), this.kendo);

    this.maximize = function () {
        this.kendo.maximize();
    }

    this.minimize = function () {
        this.kendo.minimize();
    }

    this.restore = function () {
        this.kendo.restore();
    }

    this.enableButton = function (name) {
        this.footer.find(".k-window-button [name=" + name + "]").data("kendoButton").enable(true);
    }
    this.disableButton = function (name) {
        this.footer.find(".k-window-button [name=" + name + "]").data("kendoButton").enable(false);
    }

    if (XBox.isFunction(this.options.close)) {
        var onclose = this.options.close.bind(this);
        if (this.options.destroyWhenClose) {
            onclose = XBox.Function.postFunction(onclose, this.destroy, this);
        }
        this.kendo.setOptions({ close: onclose });
    }

    if (XBox.isFunction(this.options.open)) {
        var onopen = this.options.open.bind(this);
        this.kendo.setOptions({
            open: onopen,
        });
    }

    if (this.options.autoOpen) {
        this.open();
    }
}

XBox.window.create = function (options) {
    return new XBox.window(options);
}